<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <#include "/include/head_common.ftl">
    <#include "/include/login_trace.ftl">
    <script src="${rootUrl}/jquery-validation/1.11.0/localization/messages_zh.js" type="text/javascript"></script>
    <title>sql查询</title>
    <script type="text/javascript">
      //导出功能
      function exportExcel() {
          // $("#sqlForm")[0].reset();
          var sql = $('#sqlTextArea').val().replace(/(^\s*)|(\s*$)/g, "");
          if (sql.length === 0) {
              top.$.jBox.tip("输入的SQL为空", "error", {
                  persistent : true,
                  opacity : 0,
                  timeout: 1000
              });
          } else {
              var params = $("#sqlForm").serialize();
              var downloadWindow = window.open('_blank');
              downloadWindow.location = '${rootUrl}/sql/customExport?' + params;
          }
      }
      $(document).ready(
			function() {
                closeTip();
				$("#sqlForm").validate({
				    rules:{
				        "sql":{required: true}
                    },
                    messages: {
                        "sql": "请输入SQL"
                    }
				});
			}
	  );
      function detail(index,head,json){
          $("#detail_index").val(index);
          $("#detail_head").val(head);
          $("#detail_json").val(json);
          $("#detailForm").submit();
          return false;
      }

    </script>
</head>
<body>
<ul class="nav nav-tabs">
    <li><a href="${rootUrl}/sql/query">sql查询</a></li>
    <li class="active"><a href="${rootUrl}/sql/custom">自定义查询</a></li>
</ul>
<form id="detailForm" action="${rootUrl}/sql/detail" style="display: none"
      method="post" class="breadcrumb form-search form-signin">
    <input type="hidden" id="detail_index" name="index" value="">
    <input type="hidden" id="detail_head" name="head" value="">
    <input type="hidden" id="detail_json" name="json" value="">
</form>
<div align="center">
    <form id="sqlForm" action="${rootUrl}/sql/custom"
          method="post" class="breadcrumb form-search form-signin">
        <#assign oneRow = 3 />
        <table id="contentTable"
               class="table-striped  table-condensed">
            <tr>
                <td align="center">
                    <textarea id = "sqlTextArea" rows="3" cols="5000" name="sql" class="required"
                              style="width: 100%; font-size: large; color: #0000cc">${sql!''}</textarea>
                </td>
            </tr>
            <tr align="center">
                <td>
                    <label style="color: red">数据源：</label>
                    <select name="datasource" class="input-small">
                        <option value="">请选择</option>
                        <#list dataSourceList as source>
                            <option value="${source.name}" <#if datasource?? && datasource = source.name> selected </#if>>${source.name}</option>
                        </#list>
                    </select> &nbsp;&nbsp;&nbsp;
                    <input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>
                    &nbsp;&nbsp;&nbsp;
                    <input id="btnRest" class="btn btn-primary" type="reset" value="重置"/>
                    &nbsp;&nbsp;&nbsp;
                    <input id="btnExport" class="btn btn-primary"
                           type="button" value="导出"  onclick="return confirmx('确认要导出吗？', function(){exportExcel()})" />
                </td>
            </tr>
        </table>
    </form>
</div>
<#include "/include/message.ftl">
<#if !message??>
    <div align="center" style="color: red;">
        <strong><#if (tbody?size > 0) >共&nbsp;${tbody?size}&nbsp;条 <#else> 无数据</#if></strong>
    </div>
    <table id="contentTable" class="table table-striped table-bordered table-condensed">
        <thead>
        <tr>
            <#list thead as head>
                <th style="text-align: center;">${head}</th>
            </#list>
        </tr>
        </thead>
        <tbody>
        <#assign index = 1 />
        <#list tbody as body>
            <tr>
                <#assign tdIndex = 0/>
                <#list thead as head>
                    <#if tdIndex == 0 >
                        <#assign ahref= fm.urlEncode(body.json!'')/>
                        <td style="text-align: center;"><a onclick="detail(${index},'${theadSplit!''}','${body['json']!''}')">${body[head]!''} </a></td>
                    <#else>
                        <td style="text-align: center;">${body[head]!''}</td>
                    </#if>
                    <#assign tdIndex = tdIndex + 1/>
                </#list>
                <#assign index = index + 1 />
            </tr>
        </#list>
        </tbody>
    </table>
</#if>
</body>
</html>